import type { Router } from 'vue-router'
import { useAuthStoreWithout } from '@/store/modules/auth'



const authStore = useAuthStoreWithout()

function GetQueryValue(queryName: any){
  let query = decodeURI(window.location.search.substring(1));
  let vars = query.split("&");
  for (let i = 0; i < vars.length; i++) {
    let pair = vars[i].split("=");
    if (pair[0] === queryName) {
      return pair[1];
    }
  }
  return '';
}
export function setupPageGuard(router: Router) {
  router.beforeEach(async (to, from, next) => {
    
    let token = GetQueryValue('token')
    console.log('[ token ] >', token)
    if(GetQueryValue('token')){
      authStore.setToken(token)
    }
    next()
    // const authStore = useAuthStoreWithout()
    // if (!authStore.session) {
    //   try {
    //     const data = await authStore.getSession()
    //     if (String(data.auth) === 'false' && authStore.token)
    //       authStore.removeToken()
    //     if (to.path === '/500')
    //       next({ name: 'Root' })
    //     else
    //       next()
    //   }
    //   catch (error) {
    //     if (to.path !== '/500')
    //       next({ name: '500' })
    //     else
    //       next()
    //   }
    // }
    // else {
    //   next()
    // }
  })
}
